//
//  RefreshFooter.m
//  Footer
//
//  Created by leotao on 16/12/17.
//  Copyright © 2016年 leotao. All rights reserved.
//

#import "RefreshFooter.h"
#import "Masonry.h"

@interface RefreshFooter ()

@property (strong, nonatomic) UIActivityIndicatorView *indicatorView;
@property (strong, nonatomic) UILabel *statusLabel;

@end

@implementation RefreshFooter

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        self.statusLabel = [UILabel new];
        self.statusLabel.textColor = [UIColor grayColor];
        
        self.statusLabel.text = @"上拉加载";
        
        [self addSubview:self.indicatorView];
        [self addSubview:self.statusLabel];
        
        [self.statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
        }];
        
        [self.indicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.right.equalTo(self.statusLabel.mas_left).offset(-5);
        }];
        
        self.indicatorView.hidden = YES;
        
        self.state = RefreshStateIdle;
    }
    return self;
}

- (void)beginRefresh
{
    self.indicatorView.hidden = NO;
    self.statusLabel.text = @"正在加载...";
    [self.indicatorView startAnimating];
    
    
    [self.statusLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).offset(self.indicatorView.frame.size.width + 5);
        make.centerY.equalTo(self);
    }];
    
    self.state = RefreshStateRefreshing;
}

- (void)endRefrsh
{
    self.statusLabel.text = @"上拉加载";
    [self.indicatorView stopAnimating];
    self.indicatorView.hidden = YES;
    
    self.state = RefreshStateIdle;
    
    [self.statusLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
}

- (void)endRefrshWithNoMoreData
{
    self.statusLabel.text = @"没有更多数据了";
    [self.indicatorView stopAnimating];
    self.indicatorView.hidden = YES;
    
    self.state = RefreshStateNoMoreData;
    
    [self.statusLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
}

@end
